package com.rpframework.website.device.act.admin;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

import org.apache.commons.io.FilenameUtils;
import org.springframework.stereotype.Controller;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.rpframework.core.api.FileService;
import com.rpframework.module.adminbase.domain.AdminUser;
import com.rpframework.utils.DateUtils;
import com.rpframework.utils.NumberUtils;
import com.rpframework.utils.Pager;
import com.rpframework.website.device.dao.IInteractDao;
import com.rpframework.website.device.dao.ITCommentDao;
import com.rpframework.website.device.domain.Estate;
import com.rpframework.website.device.domain.Interact;
import com.rpframework.website.device.domain.TComment;
import com.rpframework.website.device.domain.User;
import com.rpframework.website.device.service.EstateService;
import com.rpframework.website.device.service.InteractService;
import com.rpframework.website.device.service.TCommentService;
import com.rpframework.website.device.service.UserService;

@Controller
@RequestMapping("/admin/interact")
public class AdminInteractAct extends AdminAct{
	@Resource InteractService service;
	@Resource FileService fileService;
	@Resource EstateService estateService;
	@Resource TCommentService TCommentService;
	@Resource public IInteractDao iDao;
	@Resource UserService UserService;
	public @Resource ITCommentDao iCommentDao;
	@SuppressWarnings({ "rawtypes", "unchecked" })
	@RequestMapping("/{type}/list")
	public String list(@PathVariable Integer type,
			@RequestParam(value="pager", required=false) Pager pager, 
			Map<Object, Object> model, RedirectAttributes attr,
			HttpSession session
			){
		if(pager == null) {
			pager = new Pager();
		}
		if(NumberUtils.isValid(type)){
			pager.getSearchMap().put("type", type.toString());
			model.put("type", type);
		}
		pager = service.getpager(pager);
		model.put("pager", pager);
		return this.doPackageURI("interact/list");
	}
	
	@RequestMapping("/edit-{id}")
	public String edit(@PathVariable Integer id, Map<Object, Object> model,RedirectAttributes attr){
		Interact t = service.select(id);
		Assert.notNull(t,this.getClass().getName() + " Id 异常");
		model.put("pager", t);
		return this.add(attr, model);
	}
	
	@RequestMapping("/add")
	public String add(RedirectAttributes attr,Map<Object, Object> model){
		return this.doPackageURI("interact/edit");
	}
			
	@RequestMapping("/delete-{id}-{type}")
	public String delete(@PathVariable Integer id ,@PathVariable Integer type ,RedirectAttributes attr){
		boolean flag = service.delete(id);
		if(flag){
			setInfoMsg("删除成功！", attr);
		}else{
			setInfoMsg("删除失败！", attr);
		}
		return redirect("/admin/interact/"+type+"/list");
	}
			
	@RequestMapping("/dosave")
	public String doSaveOrUpdate(@ModelAttribute Interact t,
			@RequestParam(value="coverPath", required=false) MultipartFile coverPath,
			HttpSession session,
			RedirectAttributes attr){
		if(coverPath!=null && coverPath.getSize() > 0) {
			try {
				String relativelyMaplinePath = "/device/interact/" + DateUtils.nowDate(DateUtils.YYYYMMDDHHMMSS) + NumberUtils.random() + "." + FilenameUtils.getExtension(coverPath.getOriginalFilename());
				fileService.saveFile(coverPath.getInputStream(), relativelyMaplinePath); 
				t.setUrlList(relativelyMaplinePath);
			} catch (Exception e) {
				throw new IllegalArgumentException("文件上传失败，原因:" + e.getLocalizedMessage());
			}
		} else {
			
		}
			User e = UserService.select(1);
			if(e!=null){
			t.setVillage(e.getRealName());//存入地址
			t.setUserId(1);}
		boolean flag = service.doSaveOrUpdate(t);
		if(flag){
			if(NumberUtils.isValid(t.getId())) {
				setInfoMsg("更新操作成功！", attr);
			} else {
				setInfoMsg("新增操作成功！", attr);
			}
		}else{
			setInfoMsg("操作失败！", attr);
		}
		return redirect("/admin/interact/"+t.getType()+"/list");
	}
	
	
	@RequestMapping("/on")
	public @ResponseBody JsonElement onState(@RequestParam(value="id", required=false) Integer id){
		JsonObject json = new JsonObject();
		boolean flag = service.doUpdateState(id);
		if(flag){
			json.addProperty("succ", true);
		}else{
			json.addProperty("succ", false);
		}
		return json;
	}
	@RequestMapping("/off")
	public @ResponseBody JsonElement offState(@RequestParam(value="id", required=false) Integer id){
		JsonObject json = new JsonObject();
		boolean flag = service.doUpdateState(id);
		if(flag){
			json.addProperty("succ", true);
		}else{
			json.addProperty("succ", false);
		}
		return json;
	}
	
	/**
	 * 查询当前id评论内容
	 * @param id
	 * @param model
	 * @param attr
	 * @return
	 */
	@RequestMapping("/comment-{id}")
	public String comment(@PathVariable Integer id, 
			Map<Object, Object> model,RedirectAttributes attr){
	
		List<TComment> t =iCommentDao.comments(id);
		Assert.notNull(t,this.getClass().getName() + " Id 异常");
		model.put("list", t);
		return this.doPackageURI("comment/list");
	}

}
